<?php

if ( ! defined("IN_OP") )
{
    exit("You can't access this file directly...");
}
require_once( "class_3d/CheckMode_3d.php" );
$gametype = intval(getvar("gametype"));
$chk = new CheckMode_3d($curruser['acl'] , 5 , $gametype);
$chk->checkShow3dPage();
unset($chk);
$tbl = $g_gametype_3d[$gametype]['en'];
$tblName = $g_gametype_3d[$gametype]['ch'];
$showPage = true;
if ( 20 < $gametype )
{
    $_8hFlag = true;
}
else
{
    $_8hFlag = false;
}
require_once( "class_5h/RateConfig_5h.php" );
require_once( "class_5h/SRate_5h.php" );
$order = getvar("order");
$split = getvar("split");
$xyid = intval(getvar("xyid"));
$disk = intval(getvar("disk"));
$ratemod = getvar("ratemod");
$playtype = getvar("playtype");
$lotto_id_5h = getvar("lotto_id_5h");
$subid=0;
switch ( $playtype )
{
    case "rp1" :
        $subid = 1;
        break;
    case "rp2" :
        $subid = 2;
        break;
    case "rp3" :
        $subid = 3;
        break;
    case "rp4" :
        $subid = 4;
        break;
    case "rp5" :
        $subid = 5;
        break;
    default :
        $subid = intval(getvar("subid"));
        break;
}

if ( $_8hFlag )
{
    switch ( $playtype )
    {
        case "rp6" :
            $subid = 6;
            break;
        case "rp7" :
            $subid = 7;
            break;
        case "rp8" :
            $subid = 8;
            break;
    }
}

if ( $lotto_id_5h == "" )
{
    header("Cache-Control: no-cache, must-revalidate");
    header("Content-Type:text/plain");
    echo "{\"errormsg\":\"Error Round\"}";
    $showPage = false;
    exit();
}
else if ( $lotto_id_5h != 0 )
{
    $lotto_id_5h = intval($lotto_id_5h);
}
if ( $showPage )
{
    $RateConfig_5h = new RateConfig_5h($gametype);
    $SRate_5h = new SRate_5h($gametype);
    if ( ! isset($curruser["disk{$disk}"]) || $curruser["disk{$disk}"] == 0 )
    {
        header("Cache-Control: no-cache, must-revalidate");
        header("Content-Type:text/plain");
        echo "{\"errormsg\":\"Error Disk\"}";
        exit();
    }
    $split = isset($RateConfig_5h->splitArr[$split])?$split:0;
    $order = isset($RateConfig_5h->orderArr[$order])?$order:0;
    $ratemod = isset($RateConfig_5h->ratemodArr[$ratemod])?$ratemod:2;
    $playtype = isset($RateConfig_5h->playtypeArr[$playtype])?$playtype:"r1";
    $showSubid = isset($RateConfig_5h->subArr[$subid])?$subid:"0";
    if ( $disk == 0 )
    {
        $diskStr = "a";
    }
    else
    {
        $diskStr = isset($RateConfig_5h->diskArr[$disk])?$RateConfig_5h->diskArr[$disk]:"a";
    }
    $sObj = NULL;
    $rateArr = NULL;
    $tempStr = "";
    if ( in_array($playtype , $RateConfig_5h->showAllBetArr) )
    {
        $bid = intval(getvar("bid"));
        if ( in_array($bid , $RateConfig_5h->playGroupArr[$playtype]) )
        {
            $playArr = array ( $bid );
        }
        else
        {
            $playArr = array ( $RateConfig_5h->playGroupArr[$playtype][0] );
        }
    }
    else
    {
        $playArr = $RateConfig_5h->playGroupArr[$playtype];
    }
    $showAllSubArr = $RateConfig_5h->showAllSubArr;
    $tempStr = "{";
    $SRate_5h->result_type = MYSQL_ASSOC;
    if ( $gametype == 24 )
    {
        foreach ( $playArr as $btype )
        {
            $subid = 1;
            $subEnd = 2;
            switch ( $btype )
            {
                case 3:
                case 4:
                case 5:
                    $subid = 0;
                    $subEnd = 1;
                    break;
                case 6:
                    $subidArr = array ( 1 , 2 , 3 , 4 , 5,6);
                    $subEnd = 6;
                    break;
                default:
                    $subEnd = 11;
                    break;
            }
            while ( $subid < $subEnd )
            {
                $val_subid = $btype."_".$subid;
                $rateArr = $SRate_5h->getRates($lotto_id_5h , $xyid , $disk , $btype , $subid);
                $user_id = $SRate_5h->getAcl5ID($curruser['id'] , $curruser['acl']);
                if ( $rateArr2 = $SRate_5h->getRatesS($lotto_id_5h , $btype , $user_id , $subid) )
                {
                    foreach ( $rateArr2 as $key2 => $val2 )
                    {
                        $rateArr -= $key2;
                    }
                }
                $numArr = array ( );
                $valArr = array ( );
                $betNumGroup = $RateConfig_5h->getBetTypeNumber($btype);
                foreach ( $betNumGroup as $betNumGroupval )
                {
                    $rateIdx = "r".str_pad($betNumGroupval , 2 , "0" , STR_PAD_LEFT);
                    if ( isset($rateArr[$rateIdx]) )
                    {
                        array_push($numArr , $betNumGroupval);
                        array_push($valArr , $rateArr[$rateIdx]);
                    }
                }
                $tempStr .= "\"b".$val_subid."\":{\"number\":[\"".implode("\",\"" , $numArr)."\"],\"rates\":[".implode("," , $valArr)."]}, ";
                ++ $subid;
            }
        }
    }
    else
    {
        $subidBit = false;
        foreach ( $playArr as $btype )
        {
            if ( $playtype == "r1" )
            {
                switch ( $btype )
                {
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                        $subid = 1;
                        $subEnd = $gametype == 20?6:9;
                        $subidBit = true;
                        break;
                    default:
                        $subid = 0;
                        $subEnd = 1;
                        break;
                }
            }
            else
            {
                $subid = $showSubid;
                $subEnd = $subid + 1;
            }
            while ( $subid < $subEnd )
            {
                if ( $subidBit )
                {
                    $val_subid = $btype."_".$subid;
                }
                else
                {
                    $val_subid = $btype;
                }
                $rateArr = $SRate_5h->getRates($lotto_id_5h , $xyid , $disk , $btype , $subid);
                $user_id = $SRate_5h->getAcl5ID($curruser['id'] , $curruser['acl']);
                if ( $rateArr2 = $SRate_5h->getRatesS($lotto_id_5h , $btype , $user_id , $subid) )
                {
                    foreach ( $rateArr2 as $key2 => $val2 )
                    {
                        $rateArr -= $key2;
                    }
                }
                $numArr = array ( );
                $valArr = array ( );
                $betNumGroup = $RateConfig_5h->getBetTypeNumber($btype);
                foreach ( $betNumGroup as $betNumGroupval )
                {
                    $rateIdx = "r".str_pad($betNumGroupval , 2 , "0" , STR_PAD_LEFT);
                    if ( isset($rateArr[$rateIdx]) )
                    {
                        array_push($numArr , $betNumGroupval);
                        array_push($valArr , $rateArr[$rateIdx]);
                    }
                }
                $tempStr .= "\"b".$val_subid."\":{\"number\":[\"".implode("\",\"" , $numArr)."\"],\"rates\":[".implode("," , $valArr)."]}, ";
                ++ $subid;
            }
        }
    }
    if ( $_8hFlag && $playtype == "rc" )
    {
		$subid=0;
        $rcBetArr = array ( 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 );
        $tempStr .= "\"mbBet\":[";
        foreach ( $rcBetArr as $btype )
        {
            $rateArr = $SRate_5h->getRates($lotto_id_5h , $xyid , $disk , $btype , $subid);
            $user_id = $SRate_5h->getAcl5ID($curruser['id'] , $curruser['acl']);
            if ( $rateArr2 = $SRate_5h->getRatesS($lotto_id_5h , $btype , $user_id , $subid) )
            {
                foreach ( $rateArr2 as $key2 => $val2 )
                {
                    $rateArr -= $key2;
                }
            }
            $numArr = array ( );
            $betNumGroup = $RateConfig_5h->getBetTypeNumber($btype);
            foreach ( $betNumGroup as $betNumGroupval )
            {
                $rateIdx = "r".str_pad($betNumGroupval , 2 , "0" , STR_PAD_LEFT);
                if ( isset($rateArr[$rateIdx]) )
                {
                    $numArr[] = $rateArr[$rateIdx];
                }
            }
            sort($numArr);
            $tempStr .= "{\"ratesS\":\"".floatval($numArr[0])."\", \"ratesM\":\"".floatval(end($numArr))."\", \"rbet\":\"".$btype."\"}, ";
        }
        if ( substr($tempStr , 0 - 2) == ", " )
        {
            $tempStr = substr($tempStr , 0 , strlen($tempStr) - 2);
        }
        $tempStr .= "]";
    }
    if ( substr($tempStr , 0 - 2) == ", " )
    {
        $tempStr = substr($tempStr , 0 , strlen($tempStr) - 2);
    }
    $tempStr .= "}";
    header("Cache-Control: no-cache, must-revalidate");
    //header("Content-Type: text/plain");
    echo $tempStr;
}